﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App.QuanLy
{
    public partial class QuanLyGiaoVien : Form
    {
        public QuanLyGiaoVien()
        {
            InitializeComponent();
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Format = "dd/MM/yyyy";
            ngaySinhDataGridViewTextBoxColumn.DefaultCellStyle = style;
            
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLTH.tbl_HinhThucCanBo' table. You can move, or remove it, as needed.
            refreshData();
        }

        private void refreshData()
        {
            this.tbl_GiaoVienTableAdapter.Connection.ConnectionString =
                this.tbl_HinhThucCanBoTableAdapter.Connection.ConnectionString = 
                    PublicLib.getStrConn();

            this.tbl_HinhThucCanBoTableAdapter.Fill(this.qLTH.tbl_HinhThucCanBo);
            this.tbl_GiaoVienTableAdapter.FillByFlag(this.qLTH.tbl_GiaoVien, PConst.FLAG_NONE);

            reloadData();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 20;
            dg_DanhSach.Height = this.Height - pn_TimKiem.Height - 50;

            pn_TimKiem.Top = pn_Button.Top = dg_DanhSach.Bottom + 5;
            pn_Button.Left = dg_DanhSach.Right - pn_Button.Width;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            reloadData();
        }

        private void reloadData()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_MaGV.Name].Value = PublicLib.formatGVId(
                    int.Parse(dg_DanhSach.Rows[i].Cells[cl_Id.Name].Value.ToString()));
                dg_DanhSach.Rows[i].Cells[cl_CanBo.Name].Value =
                    qLTH.tbl_HinhThucCanBo.Select(qLTH.tbl_HinhThucCanBo.idColumn.ColumnName + "=" +
                        dg_DanhSach.Rows[i].Cells[canBoDataGridViewTextBoxColumn.Name].Value.ToString())[0][qLTH.tbl_HinhThucCanBo.TenColumn].ToString();
            }
        }

        private void dg_DanhSach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == cl_MaGV.Index)
            {
                switch (cl_MaGV.HeaderCell.SortGlyphDirection)
                {
                    case SortOrder.None:
                    case SortOrder.Descending:
                        dg_DanhSach.Sort(cl_Id, ListSortDirection.Ascending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                        break;

                    case SortOrder.Ascending:
                        dg_DanhSach.Sort(cl_Id, ListSortDirection.Descending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                        break;
                }
            }
            else
            {
                cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.None;
                if (e.ColumnIndex == cl_CanBo.Index)
                {
                    switch (cl_CanBo.HeaderCell.SortGlyphDirection)
                    {
                        case SortOrder.None:
                        case SortOrder.Descending:
                            dg_DanhSach.Sort(canBoDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                            cl_CanBo.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                            break;

                        case SortOrder.Ascending:
                            dg_DanhSach.Sort(canBoDataGridViewTextBoxColumn, ListSortDirection.Descending);
                            cl_CanBo.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                            break;
                    }
                }
                else
                    cl_CanBo.HeaderCell.SortGlyphDirection = SortOrder.None;
            }
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditGiaoVien frm_ThemGV = new AddEditGiaoVien();
            frm_ThemGV.Show();
            frm_ThemGV.Disposed += new EventHandler(frm_ThemGV_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            AddEditGiaoVien frm_EditGV = new AddEditGiaoVien();
            frm_EditGV.Show();
            frm_EditGV.setEditInfo(int.Parse(dg_DanhSach.SelectedRows[0].Cells[cl_Id.Name].Value.ToString()));
            frm_EditGV.Disposed += new EventHandler(frm_ThemGV_Disposed);
        }

        private void frm_ThemGV_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bt_Sua_Click(bt_Sua, new EventArgs());
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;

            deleteGV();
            refreshData();
        }

        private void deleteGV()
        {
            String idGiaoVien = dg_DanhSach.SelectedRows[0].Cells[cl_Id.Name].Value.ToString(),
                   idUser = dg_DanhSach.SelectedRows[0].Cells[userid.Name].Value.ToString();

            DataRow gv = qLTH.tbl_GiaoVien.Select(qLTH.tbl_GiaoVien.idColumn.ColumnName + "=" + idGiaoVien)[0];
            gv[qLTH.tbl_GiaoVien.flagColumn] = PConst.FLAG_DELETE;

            tbl_GiaoVienTableAdapter.Update(gv);

            tbl_UserTableAdapter adap = new tbl_UserTableAdapter();
            adap.Connection.ConnectionString = PublicLib.getStrConn();

            adap.Fill(qLTH.tbl_User);
            DataRow user = qLTH.tbl_User.Select(qLTH.tbl_User.idColumn.ColumnName + "=" + idUser)[0];
            user[qLTH.tbl_User.flagColumn] = PConst.FLAG_DELETE;
            adap.Update(user);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteGV();
        }
    }
}
